package com.ymatou.iapi.app.keyword.service;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymt.base.YmatouBaseCall;
import com.ymttest.utils.EnvSetup;

public class ProductKeywordCall extends YmatouBaseCall{

	private static String opurl ="search/productkeyword";
	public ProductKeywordCall() {
		super(EnvSetup.getSystemUrl("productkeyword"),opurl,"GET","PARAM");
	}

	/**
	 * 获取是否成功
	 * 
	 * @return String OK
	 */
	public String getStatus() {
		String OK = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			OK = jo.getString("status");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return OK;
	}
	
	/**
	 * 获取接口返回总记录数
	 * @return int 
	 */
	public String getTotal(){
		String totals = "0";
		try {
			JSONObject jo = new JSONObject(getReturnData());
			totals = jo.getString("total");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return totals;
		
	}
	
	
	/**
	 * 得到接口公共返回值，如took，requestid，algorithm
	 * @return String 
	 * */
	public String getCommonInfo(String p) {
		String params = null;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			params = jo.getString(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return params;
	}
	
	/**
	 * 得到接口返回的boolean类型的参数
	 * @return boolean
	 * */
	public boolean getCommonInfoBoo(String p) {
		boolean paramBoolean = true;
		try {
			JSONObject jo = new JSONObject(getReturnData());
			paramBoolean = jo.getBoolean(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramBoolean;
	}
	
	/**
	 * 得到hit里的数据
	 * */
	public JSONArray getHits(){
		JSONArray list = null;
		try {
			JSONObject jsonsList = new JSONObject(getReturnData());
			list = jsonsList.getJSONArray("hits");
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	
	/**
	 * 得到stats里的聚合结果
	 * */
	public JSONObject getStats(){
		JSONObject list = null;
		try {
			JSONObject jsonsList = new JSONObject(getReturnData());
			list = jsonsList.getJSONObject("stats");
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;	
	}
	
	/**
	 * 得到一个json串中的键值
	 * */
	public String[] analyzeJsonToArray(JSONObject jsonject, String type) {

		String string = jsonject.toString();
		string = string.replace("}", "");
		string = string.replace("{", "");
		string = string.replace("\"", "");
		String[] strings = string.split(",");

		if (type.equals("key")) {
			String[] stringsNum = new String[strings.length];
			for (int i = 0; i < strings.length; i++) {
				stringsNum[i] = strings[i].split(":")[0];
			}
			return stringsNum;
		} else if (type.equals("value")) {
			String[] stringsName = new String[strings.length];
			for (int i = 0; i < strings.length; i++) {
				stringsName[i] = strings[i].split(":")[1];
			}
			return stringsName;
		} else {
			return null;
		}
	}
	
}
